home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / tcoop.arc / TCOOP2.ARC / EXPENSE3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-26  |  3.0 KB  |  98 lines

  1. // expense3.cpp -- The object-oriented version of the travel
  2. // expense program that uses inheritance to handle executive
  3. // employees differently
  4. //
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <conio.h>
  9.  
  10. class Expense {             // The trip expense object type
  11. public:
  12.   char TripName[80], Date[80];
  13.   int Mileage, Cost, Fringe;
  14.   virtual void AskQuestions(void);   // Use virtual because we'll
  15.   virtual void CheckWithBoss(void);  // override in ExecutiveExp
  16. };
  17.  
  18. class ExecutiveExp : public Expense {
  19. public:
  20.   int Rating;
  21.   virtual void AskQuestions(void);   // The virtual keyword is
  22.   virtual void CheckWithBoss(void);  // optional here
  23. };
  24.  
  25. void Expense::AskQuestions(void)
  26. // This function gathers data for the expense object
  27. {
  28.   printf("Please enter the name of your trip: ");
  29.   scanf("%s", TripName);
  30.   printf("Which date did you leave (mo/day/year)? ");
  31.   scanf("%s", Date);
  32.   printf("Enter the number of miles for your trip: ");
  33.   scanf("%d", &Mileage);
  34.   printf("Enter the cost of your trip: ");
  35.   scanf("%d", &Cost);
  36.   printf("Enter the cost of your meals and entertainment: ");
  37.   scanf("%d", &Fringe);
  38. }
  39.  
  40. void Expense::CheckWithBoss(void)
  41. // The boss is very tough. He won't approve every trip.
  42. {
  43.   if (!stricmp(TripName,"Hawaii") || !stricmp(TripName,"Tahiti"))
  44.     printf("%s: You are fired for trying to sneak this one by\n",
  45.            TripName);
  46.   else if (Cost / 4 < Fringe)
  47.     printf("The fringe expenses for the %s trip are too high\n",
  48.            TripName);
  49.   else
  50.     printf("The %s trip is ok\n", TripName);
  51. }
  52.  
  53. void ExecutiveExp::AskQuestions(void)
  54. // This function gathers data for the executive expense object
  55. {
  56.   Expense::AskQuestions();   // Call the inherited version
  57.   printf("How would you rate the trip (0-10)? ");
  58.   scanf("%d", &Rating);
  59. }
  60.  
  61. void ExecutiveExp::CheckWithBoss(void)
  62. // The boss is very easy on executives
  63. {
  64.   if (Cost / 2 < Fringe)
  65.     printf("Watch it. You're not having enough fun!\n");
  66. }
  67.  
  68. Expense *ExRec[20];      // Array of pointers to base class type
  69. int ExCount, I;
  70. unsigned char Executive; // True is user is an executive
  71.  
  72. int main(int, char *)
  73. {
  74.   // Determine if the user is an executive or not. If so,
  75.   // use the ExecutiveExp class.
  76.   printf("Are you an executive (y/n) ");
  77.   if (toupper(getch()) == 'Y') Executive = 1; else Executive = 0;
  78.   printf("\nHow many trips did you take? ");
  79.   scanf("%d", &ExCount);
  80.   // Allocate enough objects, of the correct type, for the
  81.   // number of trips taken. Which objects get placed in the
  82.   // array depend on whether the user is an executive or not.
  83.   for (I=0; I<ExCount; I++)
  84.     if (Executive)
  85.       ExRec[I] = new ExecutiveExp;
  86.     else
  87.       ExRec[I] = new Expense;
  88.   // The following are polymorphic calls. We use the same
  89.   // call to reference ExecutiveExp and Expense objects.
  90.   for (I=0; I<ExCount; I++)   // Get expense data for each object
  91.     ExRec[I]->AskQuestions();
  92.   for (I=0; I<ExCount; I++)   // Write out the boss's response
  93.     ExRec[I]->CheckWithBoss();
  94.   return 0;
  95.  
  96. }
  97.  
  98.